<template>
    <div>
        <div style="margin-top: 20px;margin-bottom: 20px">
            <van-icon name="arrow-left" size="0.33rem" @click="return_home" style="float: left;margin-left: 20px"></van-icon>
            <span style="font-size: 20px;font-family: 楷体;margin-left: 130px">修改头像</span>
        </div>
        <div align="center" style="margin-top: 30px">
            <el-form status-icon  class="demo-ruleForm" style="width: 300px">
                <el-form-item label="修改头像" >
                    <el-upload
                            class="avatar-uploader"
                            action="http://192.168.0.100:81/clms-client/client/upload"
                            :headers= "headers"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="headPic" :src="headPic" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit" >提交</el-button>
                    <el-button>重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import {updateHeadPic} from '../../request/userinfo/userinfo';
    import {user} from '../../request/userinfo/userinfo';
    export default {
        name: "",
        data() {
            return {
                headers: {
                    'Token' : sessionStorage.getItem("token")
                },
                headPic: '',
                id: this.$store.state.user.id,
            }
        },
        methods: {
            return_home() {
                this.$router.go(-1);
            },
            handleAvatarSuccess(res, file) {
                this.headPic = res;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            submit() {
                console.log(this.id);
                console.log(this.headPic);
                updateHeadPic({
                    id: this.id,
                    headPic: this.headPic
                }).then( result => {
                    if (result.code == 200) {
                        this.$toast.success("修改头像成功");
                        user().then( result => {
                            this.$store.state.user = result.object
                        })
                    } else {
                        this.$toast.fail("修改头像失败")
                    }
                })
            }
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;
    }
    .avatar {
        width: 100px;
        height: 100px;
        display: block;
    }
</style>
